package com.gin.kotlin.no_gay_alliance.fragment.main.community

import android.os.Build
import android.os.Bundle
import com.gin.kotlin.no_gay_alliance.adapter.community.ForumSubGroupAdapter
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.databinding.FragmentMainCommunityForumEntryGroupBinding
import com.gin.kotlin.no_gay_alliance.model.ForumSubGroupModel

/**
 * 版面/合集入口组 Fragment，用于首页
 */
class MainForumEntryGroupFragment : AbstractBindingFragment<FragmentMainCommunityForumEntryGroupBinding>() {

    lateinit var groups: ArrayList<ForumSubGroupModel>
    lateinit var adapter: ForumSubGroupAdapter

    /**
     * 处理参数
     * @param bundle Bundle
     */
    override fun handleArguments(bundle: Bundle) {
        this.groups = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            bundle.getParcelableArrayList(KEY_GROUP, ForumSubGroupModel::class.java)
        } else bundle.getParcelableArrayList(KEY_GROUP)) ?: throw RuntimeException("没有获取到分组数据")

        // 配置适配器
        this.adapter = ForumSubGroupAdapter(requireContext()
            ,handler!!
            ,true
            ,groups)
        this.b?.recyclerView?.adapter = this.adapter
    }

    /**
     * onCreateView的后续操作 ，如通过  [FragmentActivity.addMenuProvider] 添加菜单
     */
    override fun onCreateView() {
    }


    companion object {
        private const val KEY_GROUP = "group"
        fun newInstance(groups: ArrayList<ForumSubGroupModel>?) = MainForumEntryGroupFragment().also {
            val args = Bundle()
            args.putParcelableArrayList(KEY_GROUP, groups)
            it.arguments = args
        }
    }
}